/* This file is part of GSMC.
 * 
 * GSMC is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * GSMC is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with GSMC.  If not, see <http://www.gnu.org/licenses/>.
 */

package net.hpxn.gsmc;

import java.util.ArrayList;

import org.apache.log4j.Logger;

import net.hpxn.gsmc.config.ConfigFactory;
import net.hpxn.gsmc.config.server.ServerConfigs;
import net.hpxn.gsmc.config.server.ServerConfigs.ServerConfig;
import net.hpxn.gsmc.core.event.EventMonitor;
import net.hpxn.gsmc.core.server.Server;
import net.hpxn.gsmc.core.server.ServerFactory;
import net.hpxn.gsmc.core.server.ServerManager;
import net.hpxn.gsmc.core.www.WebServer;

public class Gsmc {

	private ServerConfigs serverConfig;
	private Logger log;

	public Gsmc() {
		log = Logger.getLogger( getClass().getCanonicalName() );
	}

	public void start() {
		System.out.println( fancyUselessLogo() );

		try {
			this.serverConfig = ConfigFactory.getServerConfigs();
		} catch ( Exception e ) {
			log.fatal( "Unable to load server configuration file: "
					+ e.getMessage() );
			System.exit( 1 );
			return;
		}

		System.out
				.println( "GSMC (" + ConfigFactory.VERSION
						+ ") - Listening on Port: "
						+ this.serverConfig.getListenPort() );
		System.out.println( "========================================="
				+ "=======================================" );

		init();

		System.out.println( "========================================="
				+ "=======================================" );
	}

	private void init() {
		ArrayList<ServerConfig> wServerConfigs = (ArrayList<ServerConfig>) this.serverConfig
				.getServerConfig();
		
		WebServer wWebServer = null;
		if ( this.serverConfig.getWwwEnabled()
				.equalsIgnoreCase( "true" ) ) {
			System.out.println( "Web Server enabled: " );
			int port = Integer.valueOf( this.serverConfig.getWwwPort() );
			wWebServer = new WebServer( port );
			System.out.println( );
		}

		System.out.println( wServerConfigs.size()
				+ " server configuration(s) detected:" );

		if ( wServerConfigs.size() <= 0 ) {
			System.out.println( "Please configure at least one server." );
			System.exit( 0 );
		}

		for ( ServerConfig wServerConfig : wServerConfigs ) {
			Server wServer = ServerFactory.createServer( wServerConfig );

			if ( wServer == null ) {
				System.out.println( "Problem loading server: "
						+ wServerConfig.getName() );
			} else {
				
				if ( wWebServer != null ) {
					wServer.setServletContext( wWebServer.getContext() );
				}
				
				ServerManager.add( wServer );

				System.out.println( " " + ServerManager.getServerList().size()
						+ ". " + "(" + wServerConfig.getGroup() + ")"
						+ wServerConfig.getName() + " ["
						+ wServerConfig.getRemoteIp() + ":"
						+ wServerConfig.getRemotePort() + "] ("
						+ wServerConfig.getType().toLowerCase() + ")" );
			}
		}

		EventMonitor wMonitor;
		try {
			wMonitor = new EventMonitor();
			wMonitor.start();
		} catch ( Exception e ) {
			log.error( "Unable to start Event Monitor: " + e.getMessage() );
			e.printStackTrace();
		}
	}

	private static String fancyUselessLogo() {
		StringBuffer wLogo = new StringBuffer();

		wLogo.append( "_______________________  ____________\n" );
		wLogo.append( "__  ____/__  ___/___   |/  /__  ____/\n" );
		wLogo.append( "_  / __  _____ \\ __  /|_/ / _  /     \n" );
		wLogo.append( "/ /_/ /  ____/ / _  /  / /  / /___   \n" );
		wLogo.append( "\\____/   /____/  /_/  /_/   \\____/   \n\n" );

		wLogo.append( "GSMC  Copyright (C) 2011  Peter D.\n" );
		wLogo.append( "This program comes with ABSOLUTELY NO WARRANTY.\n" );
		wLogo.append( "This is free software, and you are welcome to  \n" );
		wLogo.append( "redistribute it under certain conditions.\n" );
		wLogo.append( "See: http://www.gnu.org/licenses/\n\n" );

		return wLogo.toString();
	}

	public static void main( String[] args ) {
		Gsmc m = new Gsmc();
		m.start();
	}
}
